﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Pomodoro.Core.View;
using Pomodoro.Core.Model.Domain;
using System.Web.Script.Services;
using System.Web.Services;

namespace Pomodoro.Web
{
    public partial class _Default : System.Web.UI.Page, TaskView
    {

        #region TaskView Members

        public string NewTask
        {
            get
            {
                if (!CampoNovaTarefa.Text.Trim().Equals(String.Empty))
                    return CampoNovaTarefa.Text;
                else
                    return String.Empty;

            }
            set
            {
                if (!value.Trim().Equals(String.Empty))
                    CampoNovaTarefa.Text = value;
                else
                    CampoNovaTarefa.Text = String.Empty;
            }
        }

        public string Alerts
        {
            get
            {
                if (!Alertas.Text.Trim().Equals(String.Empty))
                    return Alertas.Text;
                else
                    return String.Empty;

            }
            set
            {
                if (!value.Trim().Equals(String.Empty))
                    Alertas.Text = value;
                else
                    Alertas.Text = String.Empty;
            }
        }

        public ICollection<Pomodoro.Core.Model.Domain.Task> ListTasks
        {
            get
            {
                if (ListaTarefas.DataSource != null)
                {
                    return (List<Task>)ListaTarefas.DataSource;
                }

                return new List<Task>();
            }
            set
            {
                if (value != null)
                {
                    ListaTarefas.DataSource = value;
                    ListaTarefas.DataBind();
                }
            }
        }

        #endregion

        #region Page Events
        private Pomodoro.Core.Presenter.PomodoroPresenter presenter;

        public _Default()
        {
            presenter = new Pomodoro.Core.Presenter.PomodoroPresenter(this);
        }

        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void BtnNovaTarefa_Click(object sender, EventArgs e)
        {
            presenter.AddTask();
        }

        protected void ListaTarefas_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            switch (e.CommandName) { 
                case "IniciarTarefa":
                    int taskID = Convert.ToInt32(e.CommandArgument);
                    presenter.StartTask(taskID);
                    break;
                default:
                    break;
            }
        }

        protected void ListaTarefas_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow) {
                if (Convert.ToBoolean(e.Row.Cells[3].Text))
                    e.Row.Cells[3].Text = "Concluída";
                else
                    e.Row.Cells[3].Text = "Não concluída";

                if (ListaTarefas.DataKeys[e.Row.RowIndex].Value !=null)
                {
                    string status = ListaTarefas.DataKeys[e.Row.RowIndex].Value.ToString();
                    if (Convert.ToBoolean(status))
                    {
                        ((LinkButton)e.Row.Cells[2].FindControl("LinkIniciar")).Text = "Interromper";
                        ((LinkButton)e.Row.Cells[2].FindControl("LinkIniciar")).CommandName= "Interromper";
                    }
                    else
                        ((LinkButton)e.Row.Cells[2].FindControl("LinkIniciar")).Text = "Iniciar";
                }
            }
        }

        #endregion
    }
}
